package com.ruoyi.web.controller.his;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.his.domain.assignBedDTO;
import com.ruoyi.his.service.IBedService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.MedicalOrder;
import com.ruoyi.his.service.IMedicalOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医嘱信息Controller
 * 
 * @author hzr
 * @date 2024-10-18
 */
@RestController
@RequestMapping("/his/order")
public class MedicalOrderController extends BaseController
{
    @Autowired
    private IMedicalOrderService medicalOrderService;
    @Autowired
    private IBedService bedService;
    /**
     * 查询医嘱信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedicalOrder medicalOrder)
    {
        startPage();
        List<MedicalOrder> list = medicalOrderService.selectMedicalOrderList(medicalOrder);
        return getDataTable(list);
    }

    /**
     * 导出医嘱信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:order:export')")
    @Log(title = "医嘱信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicalOrder medicalOrder)
    {
        List<MedicalOrder> list = medicalOrderService.selectMedicalOrderList(medicalOrder);
        ExcelUtil<MedicalOrder> util = new ExcelUtil<MedicalOrder>(MedicalOrder.class);
        util.exportExcel(response, list, "医嘱信息数据");
    }

    /**
     * 获取医嘱信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(medicalOrderService.selectMedicalOrderById(id));
    }

    /**
     * 新增医嘱信息
     */
    @PreAuthorize("@ss.hasPermi('his:order:add')")
    @Log(title = "医嘱信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicalOrder medicalOrder)
    {
        return toAjax(medicalOrderService.insertMedicalOrder(medicalOrder));
    }

    /**
     * 修改医嘱信息
     */
    @PreAuthorize("@ss.hasPermi('his:order:edit')")
    @Log(title = "医嘱信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicalOrder medicalOrder)
    {
        return toAjax(medicalOrderService.updateMedicalOrder(medicalOrder));
    }

    /**
     * 删除医嘱信息
     */
    @PreAuthorize("@ss.hasPermi('his:order:remove')")
    @Log(title = "医嘱信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(medicalOrderService.deleteMedicalOrderByIds(ids));
    }

    @PostMapping("/assignBed")
    public AjaxResult assignBed(@RequestBody assignBedDTO assignBedDTO) {
        Long bedId = assignBedDTO.getBedId();
        Long patientId = assignBedDTO.getPatientId();
        Long orderId = assignBedDTO.getOrderId();
        // 更新病床状态为占用
        bedService.assignBed(bedId, patientId);

        // 更新医嘱状态为已执行
        medicalOrderService.updateOrderStatusToExecuted(orderId);

        return AjaxResult.success("病床安排成功！");
    }
}
